/***************************************************************************
 * 文件名:VisitsManager.java<br>
 * 版本: <br>
 * 描述: <br>出诊管理
 * 版权所有: <br>
 * //////////////////////////////////////////////////////// <br>
 * 创建者: robo<br>
 * 创建日期: 2010-12-5 <br>
 * 修改者: <br>
 * 修改日期: <br>
 * 修改说明: <br>
 **************************************************************************//*
package com.sunwayhorizo.health.business.registries.visits;

import java.sql.*;
import java.util.*;
import java.util.Date;

import javax.ejb.*;
import javax.persistence.*;

import org.jboss.seam.*;
import org.jboss.seam.annotations.*;

import com.sunwayhorizo.health.business.base.*;
import com.sunwayhorizo.health.business.dto.*;
import com.sunwayhorizo.health.business.dto.registries.*;
import com.sunwayhorizo.health.business.patad.encounter.*;
import com.sunwayhorizo.health.business.registries.employee.EmployeeManagerLocal;
import com.sunwayhorizo.health.business.registries.organization.OrganizationManagerLocal;
import com.sunwayhorizo.health.common.BusinessConstants;
import com.sunwayhorizo.health.common.DataTypeUtil;
import com.sunwayhorizo.health.common.DateUtil;
import com.sunwayhorizo.health.common.ListUtil;
import com.sunwayhorizo.health.common.StrUtil;
import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.datatypes.CE;
import com.sunwayhorizo.health.hl7.datatypes.IVL_TS;
import com.sunwayhorizo.health.hl7.datatypes.PQ;
import com.sunwayhorizo.health.hl7.datatypes.RTO_PQ;
import com.sunwayhorizo.health.hl7.datatypes.SET_II;
import com.sunwayhorizo.health.hl7.datatypes.common.InternalFactory;
import com.sunwayhorizo.health.hl7.persistent.RimCoreEntities;
import com.sunwayhorizo.health.hl7.persistent.RimCoreEntyNameUses;
import com.sunwayhorizo.health.hl7.persistent.RimCoreEntyNames;
import com.sunwayhorizo.health.hl7.persistent.RimCoreRoleCdSets;
import com.sunwayhorizo.health.hl7.persistent.RimCoreRoles;
import com.sunwayhorizo.health.hl7.rim.Act;
import com.sunwayhorizo.health.hl7.rim.ControlAct;
import com.sunwayhorizo.health.hl7.rim.Employee;
import com.sunwayhorizo.health.hl7.rim.Organization;
import com.sunwayhorizo.health.hl7.rim.Person;
import com.sunwayhorizo.health.hl7.rim.Role;
import com.sunwayhorizo.health.hl7.vocabulary.ActCode;
import com.sunwayhorizo.health.hl7.vocabulary.ActMood;
import com.sunwayhorizo.health.hl7.vocabulary.ActRelationshipType;
import com.sunwayhorizo.health.hl7.vocabulary.NullFlavor;
import com.sunwayhorizo.health.hl7.vocabulary.ParticipationType;
import com.sunwayhorizo.health.hl7.vocabulary.RoleClass;

@Name("VisitsManager")
@Stateless
@SuppressWarnings("unchecked")
public class VisitsManager extends BaseManager implements VisitsManagerLocal
{

    public ResultsDTO newVistis(VisitsDTO visitsDTO) throws Hl7Exception
    {

        String extenID = em.createNativeQuery("select EXTEN_CODE.NEXTVAL from dual").getResultList().get(0).toString();
        SET_II roleId = getDataTypeFactory().newSET_II(root, "role" + extenID, true);
        CE ce = getDataTypeFactory().nullCE(NullFlavor.NI);

        EmployeeManagerLocal employeeManagerLocal = (EmployeeManagerLocal) Component.getInstance("EmployeeManager");

        Person person = null;
        if (StrUtil.isNotBlank(visitsDTO.getPersonId()))
        {
            Employee employee = employeeManagerLocal.findEmployeeDetail(Long.parseLong(visitsDTO.getPersonId()), null);
            person = (Person) employee.getPlayerEntities();
        }

        // 得到Depart
        OrganizationManagerLocal org = (OrganizationManagerLocal) Component.getInstance("OrganizationManager");
        Organization organization = org.findOrganizationById(Long.parseLong(visitsDTO.getDepartmentId()));

        // 创建ControlAct
        SET_II controlActIi = DataTypeUtil.getSet_II(root, "controlAct" + extenID, true, "CACT");
        ControlAct controlAct = getActFactory().newControlActEvent(getDataTypeFactory().nullCD(NullFlavor.NI), controlActIi);

        // 创建ACT
        SET_II regIi = DataTypeUtil.getSet_II(root, "reg" + extenID, true, "REG");
        Act registrationAct = getActFactory().newRegistryAct(ActMood.EVN, ActCode.CLINICINDEX, regIi);

        // 创建ControlAct与ACT的关系
        controlAct.addOBActRelationship(ActRelationshipType.SUBJ, registrationAct);

        // 给Organization赋角色(周一到周日)出诊时间【周一至周日】：前台字典【上午、下午、白天、前夜、后夜、全天，不出诊】
        DictionaryDTO dicDTO = visitsDTO.getVisitsType();
        CE visitsTypeCe = null;
        if (dicDTO != null)
        {
            visitsTypeCe = this.getDataTypeFactory().newCE(dicDTO.getConceptCode(), dicDTO.getCodingschemeOid(), dicDTO.getCodingschemeEnName(),
                dicDTO.getCodingschemeChName(), dicDTO.getConceptName(), dicDTO.getVersionName());
        }
        IVL_TS ivl_ts = null;

        PQ pq = getDataTypeFactory().newPQ(visitsDTO.getLimitedNumbers(), "人");
        PQ pq1 = getDataTypeFactory().newPQ(visitsDTO.getLimitNumberOfNum(), "人");
        RTO_PQ rtoPq = getDataTypeFactory().newRTO_PQ(pq, pq1);
        String monday = visitsDTO.getMonday();
        ce = null;
        if (StrUtil.isNotEmpty(monday))
        {
            Role role = null;
            if (person == null)
            {
                role = getRoleFactory().newClinicIndex(ce, organization, organization, roleId);
            }
            else
            {
                role = getRoleFactory().newClinicIndex(ce, person, organization, roleId);
            }
            role.setRoleName(visitsDTO.getVisitsNumber());
            ivl_ts = getIvlTsByTime(visitsDTO, ivl_ts, monday);
            // role.setEffectiveTime(getDataTypeFactory().newGTS(ivl_ts));

            role.setQuantity(rtoPq);
            role.setStatusCode(InternalFactory.newCS(visitsDTO.getStatus()));
            role.setEffectiveDateGtsTxt(monday);
            role.setPriorityNumber(DateUtil.MONDAY);
            role.setVisitType(visitsTypeCe);
            registrationAct.addParticipation(ParticipationType.SBJ, role);
        }

        String tuesday = visitsDTO.getTuesday();
        if (StrUtil.isNotEmpty(tuesday))
        {
            Role roletuesday = null;
            if (person == null)
            {
                roletuesday = getRoleFactory().newClinicIndex(ce, organization, organization, roleId);
            }
            else
            {
                roletuesday = getRoleFactory().newClinicIndex(ce, person, organization, roleId);
            }
            roletuesday.setRoleName(visitsDTO.getVisitsNumber());
            ivl_ts = getIvlTsByTime(visitsDTO, ivl_ts, tuesday);
            // roletuesday.setEffectiveTime(getDataTypeFactory().newGTS(ivl_ts));
            roletuesday.setStatusCode(InternalFactory.newCS(visitsDTO.getStatus()));
            roletuesday.setQuantity(rtoPq);
            roletuesday.setEffectiveDateGtsTxt(tuesday);
            roletuesday.setPriorityNumber(DateUtil.TUESDAY);
            roletuesday.setVisitType(visitsTypeCe);
            registrationAct.addParticipation(ParticipationType.SBJ, roletuesday);
        }

        String wednesday = visitsDTO.getWednesday();
        if (StrUtil.isNotEmpty(wednesday))
        {
            Role rolewednesday = null;
            if (person == null)
            {
                rolewednesday = getRoleFactory().newClinicIndex(ce, organization, organization, roleId);
            }
            else
            {
                rolewednesday = getRoleFactory().newClinicIndex(ce, person, organization, roleId);
            }
            rolewednesday.setRoleName(visitsDTO.getVisitsNumber());
            ivl_ts = getIvlTsByTime(visitsDTO, ivl_ts, wednesday);
            // rolewednesday.setEffectiveTime(getDataTypeFactory().newGTS(ivl_ts));
            rolewednesday.setQuantity(rtoPq);
            rolewednesday.setEffectiveDateGtsTxt(wednesday);
            rolewednesday.setStatusCode(InternalFactory.newCS(visitsDTO.getStatus()));
            rolewednesday.setPriorityNumber(DateUtil.WEDNESDAY);
            rolewednesday.setVisitType(visitsTypeCe);
            registrationAct.addParticipation(ParticipationType.SBJ, rolewednesday);
        }

        String thursday = visitsDTO.getThursday();
        if (StrUtil.isNotEmpty(thursday))
        {
            Role rolethursday = null;
            if (person == null)
            {
                rolethursday = getRoleFactory().newClinicIndex(ce, organization, organization, roleId);
            }
            else
            {
                rolethursday = getRoleFactory().newClinicIndex(ce, person, organization, roleId);
            }
            rolethursday.setRoleName(visitsDTO.getVisitsNumber());
            ivl_ts = getIvlTsByTime(visitsDTO, ivl_ts, thursday);
            // rolethursday.setEffectiveTime(getDataTypeFactory().newGTS(ivl_ts));
            rolethursday.setStatusCode(InternalFactory.newCS(visitsDTO.getStatus()));
            rolethursday.setQuantity(rtoPq);
            rolethursday.setEffectiveDateGtsTxt(thursday);
            rolethursday.setPriorityNumber(DateUtil.THURSDAY);
            rolethursday.setVisitType(visitsTypeCe);
            registrationAct.addParticipation(ParticipationType.SBJ, rolethursday);
        }

        String friday = visitsDTO.getFriday();
        if (StrUtil.isNotEmpty(friday))
        {
            Role rolefriday = null;
            if (person == null)
            {
                rolefriday = getRoleFactory().newClinicIndex(ce, organization, organization, roleId);
            }
            else
            {
                rolefriday = getRoleFactory().newClinicIndex(ce, person, organization, roleId);
            }

            rolefriday.setRoleName(visitsDTO.getVisitsNumber());
            ivl_ts = getIvlTsByTime(visitsDTO, ivl_ts, friday);
            // rolefriday.setEffectiveTime(getDataTypeFactory().newGTS(ivl_ts));
            rolefriday.setQuantity(rtoPq);
            rolefriday.setStatusCode(InternalFactory.newCS(visitsDTO.getStatus()));
            rolefriday.setEffectiveDateGtsTxt(friday);
            rolefriday.setPriorityNumber(DateUtil.FRIDAY);
            rolefriday.setVisitType(visitsTypeCe);
            registrationAct.addParticipation(ParticipationType.SBJ, rolefriday);
        }

        String saturday = visitsDTO.getSaturday();
        if (StrUtil.isNotEmpty(saturday))
        {
            Role rolesaturday = null;
            if (person == null)
            {
                rolesaturday = getRoleFactory().newClinicIndex(ce, organization, organization, roleId);
            }
            else
            {
                rolesaturday = getRoleFactory().newClinicIndex(ce, person, organization, roleId);
            }
            rolesaturday.setRoleName(visitsDTO.getVisitsNumber());
            ivl_ts = getIvlTsByTime(visitsDTO, ivl_ts, saturday);
            // rolesaturday.setEffectiveTime(getDataTypeFactory().newGTS(ivl_ts));
            rolesaturday.setStatusCode(InternalFactory.newCS(visitsDTO.getStatus()));
            rolesaturday.setQuantity(rtoPq);
            rolesaturday.setEffectiveDateGtsTxt(saturday);
            rolesaturday.setPriorityNumber(DateUtil.SATURDAY);
            rolesaturday.setVisitType(visitsTypeCe);
            registrationAct.addParticipation(ParticipationType.SBJ, rolesaturday);
        }

        String sunday = visitsDTO.getSunday();
        if (StrUtil.isNotEmpty(sunday))
        {
            Role rolesunday = null;
            if (person == null)
            {
                rolesunday = getRoleFactory().newClinicIndex(ce, organization, organization, roleId);
            }
            else
            {
                rolesunday = getRoleFactory().newClinicIndex(ce, person, organization, roleId);
            }
            rolesunday.setRoleName(visitsDTO.getVisitsNumber());
            ivl_ts = getIvlTsByTime(visitsDTO, ivl_ts, sunday);
            // rolesunday.setEffectiveTime(getDataTypeFactory().newGTS(ivl_ts));
            rolesunday.setStatusCode(InternalFactory.newCS(visitsDTO.getStatus()));
            rolesunday.setQuantity(rtoPq);
            rolesunday.setEffectiveDateGtsTxt(sunday);
            rolesunday.setPriorityNumber(DateUtil.SUNDAY);
            rolesunday.setVisitType(visitsTypeCe);
            registrationAct.addParticipation(ParticipationType.SBJ, rolesunday);
        }

        em.persist(controlAct);
        ResultsDTO resultsDTO = getResultsDTO("保存号表成功", true, "");
        log.info("查询到数据库记录条数：" + visitsDTO.getCurrentNumber());
        return resultsDTO;
    }


    public ResultsDTO updateVisits(VisitsDTO visitsDTO) throws Hl7Exception
    {

        ResultsDTO resultsDTO = getResultsDTO("更新成功", true, "");
        if (StrUtil.isNotBlank(visitsDTO.getPersonId()))
        {
            StringBuilder sb = new StringBuilder(" select o from Role o where o.playerEntities.entityId=" + visitsDTO.getPersonId()
                    + " and o.classCode='" + RoleClass.CLINICINDEX.toString() + "' ");
            List<Role> list = em.createQuery(sb.toString()).getResultList();
            String monday = visitsDTO.getMonday();
            String tuesday = visitsDTO.getTuesday();
            String wednesday = visitsDTO.getWednesday();
            String thursday = visitsDTO.getThursday();
            String friday = visitsDTO.getFriday();
            String saturday = visitsDTO.getSaturday();
            String sunday = visitsDTO.getSunday();
            if (ListUtil.isNotEmpty(list))
            {
                for (Role role : list)
                {

                    String number = role.getPriorityNumber();
                    if (StrUtil.isNotBlank(number))
                    {
                        if (number.equals(DateUtil.MONDAY))
                        {
                            role.setPriorityNumber(monday);
                        }
                        else if (number.equals(DateUtil.TUESDAY))
                        {
                            role.setPriorityNumber(tuesday);
                        }
                        else if (number.equals(DateUtil.WEDNESDAY))
                        {
                            role.setPriorityNumber(wednesday);
                        }
                        else if (number.equals(DateUtil.THURSDAY))
                        {
                            role.setPriorityNumber(thursday);
                        }
                        else if (number.equals(DateUtil.FRIDAY))
                        {
                            role.setPriorityNumber(friday);
                        }
                        else if (number.equals(DateUtil.SATURDAY))
                        {
                            role.setPriorityNumber(saturday);
                        }
                        else if (number.equals(DateUtil.SUNDAY))
                        {
                            role.setPriorityNumber(sunday);
                        }
                    }
                    role.setRoleName(visitsDTO.getVisitsNumber());
                    em.merge(role);
                }
            }
        }

        return resultsDTO;
    }


    public ResultsDTO findVisits(int page, int pageSize, String name) throws Hl7Exception
    {
        String jpql = "select rol from Role rol where rol.classCode='" + RoleClass.CLINICINDEX.toString() + "' ";
        if (name != null && !name.equals(""))
        {
            jpql += " and en.formatted like '" + name + "%' ";
        }
        List<Role> list = em.createQuery(jpql).getResultList();
        ResultsDTO resultsDTO = getResultsDTO("查询成功", true, "");
        List<VisitsDTO> returnList = new ArrayList<VisitsDTO>();
        List<VisitsDTO> listVisits = new ArrayList<VisitsDTO>();
        List<VisitsDTO> commVisits = new ArrayList<VisitsDTO>();
        if (ListUtil.isNotEmpty(list))
        {
            for (int i = 0; i < list.size(); i++)
            {
                Role role = (Role) list.get(i);
                VisitsDTO visitsDTO = convertVisitsDTOByRole(role);

                boolean b = true;
                for (int j = 0; j < listVisits.size(); j++)
                {
                    VisitsDTO dto = (VisitsDTO) listVisits.get(j);
                    if (visitsDTO.getPersonId().equals(dto.getPersonId()))
                    {
                        b = false;
                        if (!visitsDTO.getVisitsId().equals(dto.getVisitsId()))
                        {
                            commVisits.add(visitsDTO);
                            break;
                        }
                    }
                }
                if (b)
                {
                    listVisits.add(visitsDTO);
                }
            }

            for (int k = 0; k < listVisits.size(); k++)
            {
                VisitsDTO visitsDTO = (VisitsDTO) listVisits.get(k);
                for (int v = 0; v < commVisits.size(); v++)
                {
                    VisitsDTO vDTO = (VisitsDTO) commVisits.get(v);
                    if (visitsDTO.getPersonId().equals(vDTO.getPersonId()))
                    {
                        visitsDTO = setEffectTimeDTO(vDTO, visitsDTO);
                    }
                }
                returnList.add(visitsDTO);
            }

        }
        resultsDTO.setObject(returnList);
        log.info("查询到数据库记录条数：" + returnList.size());
        return resultsDTO;
    }


    @SuppressWarnings("rawtypes")
    public ResultsDTO delVisits(List ids) throws Hl7Exception
    {
        StringBuilder sb = new StringBuilder("select o from RimCoreRoles o where  o.classCode='" + RoleClass.CLINICINDEX.toString() + "' ");
        if (ListUtil.isNotEmpty(ids))
        {
            sb.append(" and ( ");
            for (int i = 0; i < ids.size(); i++)
            {
                String id = (String) ids.get(i);
                if (i < ids.size() - 1)
                {
                    sb.append(" o.playerEntities.entityId=" + id + " or ");
                }
                else
                {
                    sb.append(" o.playerEntities.entityId=" + id);
                }
            }
            sb.append(" )");
        }
        List<RimCoreRoles> list = em.createQuery(sb.toString()).getResultList();
        for (int i = 0; i < list.size(); i++)
        {
            RimCoreRoles role = (RimCoreRoles) list.get(i);
            em.remove(role);
        }
        ResultsDTO resultsDTO = getResultsDTO("删除号表成功", true, "");
        return resultsDTO;
    }


    public ResultsDTO findVisitsNumberList() throws Hl7Exception
    {
        String jpql = "select rol.roleName from Role rol where rol.classCode='CLINICINDEX'";
        List<String> list = em.createQuery(jpql).getResultList();
        ResultsDTO resultsDTO = getResultsDTO("查询数据", true, list);
        return resultsDTO;
    }


    public ResultsDTO findVisitsByDate(String currentDate) throws Hl7Exception
    {
        ResultsDTO resultsDTO = getResultsDTO("查询成功", true, "");
        String jpql = "select o from Role o where o.priorityNumber =:currentDate ";
        Query query = em.createQuery(jpql);
        query.setParameter("currentDate", currentDate);
        // query.setParameter("startDate",
        // DateUtil.getDate(),TemporalType.DATE);
        // query.setParameter("endDate", DateUtil.getDate(),TemporalType.DATE);
        List<VisitsDTO> returnList = new ArrayList<VisitsDTO>();
        List<Role> list = query.getResultList();
        List<VisitsDTO> listVisits = new ArrayList<VisitsDTO>();
        List<VisitsDTO> commVisits = new ArrayList<VisitsDTO>();
        if (ListUtil.isNotEmpty(list))
        {
            for (int i = 0; i < list.size(); i++)
            {
                Role role = (Role) list.get(i);
                VisitsDTO visitsDTO = convertVisitsDTO(role);

                boolean b = true;
                for (int j = 0; j < listVisits.size(); j++)
                {
                    VisitsDTO dto = (VisitsDTO) listVisits.get(j);
                    if (visitsDTO.getPersonId().equals(dto.getPersonId()))
                    {
                        b = false;
                        if (!visitsDTO.getVisitsId().equals(dto.getVisitsId()))
                        {
                            commVisits.add(visitsDTO);
                            break;
                        }
                    }
                }
                if (b)
                {
                    listVisits.add(visitsDTO);
                }
            }

            for (int k = 0; k < listVisits.size(); k++)
            {
                VisitsDTO visitsDTO = (VisitsDTO) listVisits.get(k);
                for (int v = 0; v < commVisits.size(); v++)
                {
                    VisitsDTO vDTO = (VisitsDTO) commVisits.get(v);
                    if (visitsDTO.getPersonId().equals(vDTO.getPersonId()))
                    {
                        visitsDTO = setEffectTimeDTO(vDTO, visitsDTO);
                    }
                }
                returnList.add(visitsDTO);
            }

        }
        resultsDTO.setObject(returnList);
        return resultsDTO;
    }


    public VisitsDTO setEffectTimeDTO(VisitsDTO vDto, VisitsDTO visitsDTO)
    {

        if (StrUtil.isNotEmpty(vDto.getMonday()))
        {
            visitsDTO.setMonday(vDto.getMonday());
        }
        if (StrUtil.isNotEmpty(vDto.getTuesday()))
        {
            visitsDTO.setTuesday(vDto.getTuesday());
        }
        if (StrUtil.isNotEmpty(vDto.getWednesday()))
        {
            visitsDTO.setWednesday(vDto.getWednesday());
        }
        if (StrUtil.isNotEmpty(vDto.getThursday()))
        {
            visitsDTO.setThursday(vDto.getThursday());
        }
        if (StrUtil.isNotEmpty(vDto.getFriday()))
        {
            visitsDTO.setFriday(vDto.getFriday());
        }
        if (StrUtil.isNotEmpty(vDto.getSaturday()))
        {
            visitsDTO.setSaturday(vDto.getSaturday());
        }
        if (StrUtil.isNotEmpty(vDto.getSunday()))
        {
            visitsDTO.setSunday(vDto.getSunday());
        }
        return visitsDTO;
    }


    public VisitsDTO convertVisitsDTO(Role role) throws Hl7Exception
    {
        VisitsDTO visitsDTO = new VisitsDTO();
        visitsDTO.setVisitsNumber(role.getRoleName());
        visitsDTO.setVisitsId(role.getRoleId().toString());
        setVisitsType(role, visitsDTO);
        setDepartment(role, visitsDTO);
        setPerson(role, visitsDTO);
        visitsDTO.setLimitedNumbers("30");
        CheckinManagerLocal checkInManager = (CheckinManagerLocal) Component.getInstance("CheckinManager");
        Long currentNumber = checkInManager.findCountEncounter(String.valueOf(role.getRoleId()));
        visitsDTO.setCurrentNumber(String.valueOf(currentNumber));

        String number = role.getPriorityNumber();
        if (StrUtil.isNotEmpty(number))
        {
            if (number.equals(DateUtil.MONDAY))
            {
                visitsDTO.setMonday(role.getEffectiveDateGtsTxt());
            }
            else if (number.equals(DateUtil.TUESDAY))
            {
                visitsDTO.setTuesday(role.getEffectiveDateGtsTxt());
            }
            else if (number.equals(DateUtil.WEDNESDAY))
            {
                visitsDTO.setWednesday(role.getEffectiveDateGtsTxt());
            }
            else if (number.equals(DateUtil.THURSDAY))
            {
                visitsDTO.setThursday(role.getEffectiveDateGtsTxt());
            }
            else if (number.equals(DateUtil.FRIDAY))
            {
                visitsDTO.setFriday(role.getEffectiveDateGtsTxt());
            }
            else if (number.equals(DateUtil.SATURDAY))
            {
                visitsDTO.setSaturday(role.getEffectiveDateGtsTxt());
            }
            else if (number.equals(DateUtil.SUNDAY))
            {
                visitsDTO.setSunday(role.getEffectiveDateGtsTxt());
            }

        }
        visitsDTO.setStatus(role.getStatusCode().toString());
        visitsDTO.setLimitedNumbers(role.getQtyNumerTxt());
        visitsDTO.setLimitNumberOfNum(role.getQtyDenomTxt());
        visitsDTO.setRegisterFee("2");
        visitsDTO.setTreatmentFee("10");
        return visitsDTO;
    }


    private VisitsDTO convertVisitsDTOByRole(Role role) throws Hl7Exception
    {
        VisitsDTO visitsDTO = new VisitsDTO();
        visitsDTO.setVisitsNumber(role.getRoleName());
        visitsDTO.setVisitsId(role.getRoleId().toString());
        setVisitsType(role, visitsDTO);
        setDepartment(role, visitsDTO);
        setPerson(role, visitsDTO);
        String number = role.getPriorityNumber();
        if (StrUtil.isNotEmpty(number))
        {
            if (number.equals(DateUtil.MONDAY))
            {
                visitsDTO.setMonday(role.getEffectiveDateGtsTxt());
            }
            else if (number.equals(DateUtil.TUESDAY))
            {
                visitsDTO.setTuesday(role.getEffectiveDateGtsTxt());
            }
            else if (number.equals(DateUtil.WEDNESDAY))
            {
                visitsDTO.setWednesday(role.getEffectiveDateGtsTxt());
            }
            else if (number.equals(DateUtil.THURSDAY))
            {
                visitsDTO.setThursday(role.getEffectiveDateGtsTxt());
            }
            else if (number.equals(DateUtil.FRIDAY))
            {
                visitsDTO.setFriday(role.getEffectiveDateGtsTxt());
            }
            else if (number.equals(DateUtil.SATURDAY))
            {
                visitsDTO.setSaturday(role.getEffectiveDateGtsTxt());
            }
            else if (number.equals(DateUtil.SUNDAY))
            {
                visitsDTO.setSunday(role.getEffectiveDateGtsTxt());
            }

        }
        visitsDTO.setStatus(role.getStatusCode().toString());
        visitsDTO.setLimitedNumbers(role.getQtyNumerTxt());
        visitsDTO.setLimitNumberOfNum(role.getQtyDenomTxt());
        visitsDTO.setRegisterFee("2");
        visitsDTO.setTreatmentFee("10");
        return visitsDTO;
    }


    public ResultsDTO findVisitsTimeList() throws Hl7Exception
    {
        List<String> list = BusinessConstants.visitsTimeList;
        ResultsDTO resultsDTO = getResultsDTO("查询成功", true, list);
        return resultsDTO;
    }


    private void setPerson(Role role, VisitsDTO visitsDTO)
    {

        RimCoreEntities entities = role.getPlayerEntities();
        visitsDTO.setPersonId(entities.getEntityId().toString());
        setPersonName(visitsDTO, entities);
    }


    private void setPersonName(VisitsDTO visitsDTO, RimCoreEntities entities)
    {
        List<RimCoreEntyNames> enNames = entities.getEntyNamesList();
        if (ListUtil.isNotEmpty(enNames))
        {
            for (RimCoreEntyNames name : enNames)
            {
                List<RimCoreEntyNameUses> userList = name.getEntyNameUsesList();
                RimCoreEntyNameUses users = null;
                if (ListUtil.isNotEmpty(userList))
                {
                    users = (RimCoreEntyNameUses) userList.get(0);
                }
                if ((users != null) && users.getUse().equals(BusinessConstants.USECODE_EN_LEGAL))
                {
                    visitsDTO.setPersonName(name.getFormatted());
                    break;
                }
            }
        }
    }


    private void setDepartment(Role role, VisitsDTO visitsDTO)
    {
        RimCoreEntities entities = role.getScoperEntities();
        visitsDTO.setDepartmentId(entities.getEntityId().toString());
        setName(visitsDTO, entities);
    }


    private void setName(VisitsDTO visitsDTO, RimCoreEntities entities)
    {
        List<RimCoreEntyNames> enNames = entities.getEntyNamesList();
        if (ListUtil.isNotEmpty(enNames))
        {
            for (RimCoreEntyNames name : enNames)
            {
                List<RimCoreEntyNameUses> userList = name.getEntyNameUsesList();
                RimCoreEntyNameUses users = null;
                if (ListUtil.isNotEmpty(userList))
                {
                    users = (RimCoreEntyNameUses) userList.get(0);
                }

                if ((users != null) && users.getUse().equals(BusinessConstants.USECODE_EN_LEGAL))
                {
                    visitsDTO.setDepartmentName(name.getFormatted());
                    break;
                }
            }
        }
    }


    private void setVisitsType(Role role, VisitsDTO visitsDTO)
    {
        if (ListUtil.isNotEmpty(role.getRoleCdSetsList()))
        {

            // 确定一个出诊管理对应一个号类
            RimCoreRoleCdSets rcrc = role.getRoleCdSetsList().get(0);
            DictionaryDTO visitsType = new DictionaryDTO();
            visitsType.setCodingschemeChName(rcrc.getCodingschemeChName());
            visitsType.setCodingschemeEnName(rcrc.getCodingschemeEnName());
            visitsType.setCodingschemeOid(rcrc.getCodingschemeOid());
            visitsType.setConceptCode(rcrc.getConceptCode());
            visitsType.setConceptName(rcrc.getConceptName());
            visitsType.setVersionName(rcrc.getCodingschemeVersionName());
            visitsDTO.setVisitsType(visitsType);
        }
    }


    private IVL_TS getIvlTsByTime(VisitsDTO visitsDTO, IVL_TS ivl_ts, String monday) throws Hl7Exception
    {
        if (monday.equals(BusinessConstants.TIME_AM_STR))
        {
            ivl_ts = getIvlTs(monday, 8, 12);
        }
        else if (monday.equals(BusinessConstants.TIME_PM_STR))
        {
            ivl_ts = getIvlTs(monday, 12, 17);
        }
        else if (monday.equals(BusinessConstants.TIME_DAY_STR))
        {
            ivl_ts = getIvlTs(monday, 9, 17);
        }
        else if (monday.equals(BusinessConstants.TIME_EVE_STR))
        {
            ivl_ts = getIvlTs(monday, 1, 3);
        }
        else if (monday.equals(BusinessConstants.TIME_NIGHT_STR))
        {
            ivl_ts = getIvlTs(monday, 3, 6);
        }
        else if (monday.equals(BusinessConstants.TIME_NO_VISITS_STR))
        {
            ivl_ts = getIvlTs(monday, 9, 17);
        }
        return ivl_ts;
    }


    private void setEffectTime(Role role, String monday) throws Hl7Exception
    {
        if (monday.equals(BusinessConstants.TIME_AM_STR))
        {
            Timestamp start = Timestamp.valueOf("9");
            Timestamp end = Timestamp.valueOf("12");
            role.setEffectiveStartDateTs(start);

        }
        else if (monday.equals(BusinessConstants.TIME_PM_STR))
        {
            // ivl_ts = getIvlTs(monday,12,17);
        }
        else if (monday.equals(BusinessConstants.TIME_DAY_STR))
        {
            // ivl_ts = getIvlTs(monday,9,17);
        }
        else if (monday.equals(BusinessConstants.TIME_EVE_STR))
        {
            // ivl_ts = getIvlTs(monday,1,3);
        }
        else if (monday.equals(BusinessConstants.TIME_NIGHT_STR))
        {
            // ivl_ts = getIvlTs(monday,3,6);
        }
        else if (monday.equals(BusinessConstants.TIME_NO_VISITS_STR))
        {
            // ivl_ts = getIvlTs(monday,9,17);
        }

    }


    // 此方法有问题，请修改！
    private IVL_TS getIvlTs(String value, int startHour, int endHour) throws Hl7Exception
    {
        IVL_TS ivl_ts;
        GregorianCalendar startTime = DateUtil.getGregorianCalendar(startHour, 00, 00);
        GregorianCalendar endTime = DateUtil.getGregorianCalendar(endHour, 00, 00);
        Date startDate = startTime.getTime();
        Date endDate = endTime.getTime();
        ivl_ts = DataTypeUtil.getIVL_TS(startDate, endDate, value, "大庆油田总医院", new Date());
        return ivl_ts;
    }

}
*/